const BaseIpcHandler = require('../BaseIpcHandler');
const { getAllDirsInDirectory } = require('../../utils/fileUtils');

class GetSubDirectoriesHandler extends BaseIpcHandler {
    async handle(event, params) {
        // 解析参数
        // console.log('>> GetSubDirectoriesHandler params = ', params);
        const directory = params.directory;

        // 业务逻辑
        let dirList = await getAllDirsInDirectory(directory);
        let result = { code: 0, msg: dirList };
        if (dirList == null) {
            result = { code: -1, msg: `Directory ${directory} not exits!` };
        }

        // 返回结果
        if (event.from === 'localserver') {
            event.resp.send(result);
        } else {
            return result;
        }
    }
}

module.exports = GetSubDirectoriesHandler;
